<?php
include_once("adminBase.php");

class Tech extends AdminBase{
	function index(){
		$searchWhere = $this->searchWhere();
		$sql="SELECT count(*)as total FROM `tbl_tech` tech where tech.del_flg='0' $searchWhere";
		$total = $this->db->loadResult($sql);
		if(isset($_REQUEST['p']) && $_REQUEST['p'] > 0){
			$curPage = @intval($_REQUEST['p']);
		}elseif(isset($_SESSION['CURPAGE']) && $_SESSION['CURPAGE'] > 0){
			$curPage = @intval($_SESSION['CURPAGE']);
		}else{
			$curPage = 1;
		}
		$_SESSION['CURPAGE'] = $curPage;
		$showNum = _BACKVIEWNUM;
		$pageLink = "/management/admin_tech.php";
		$page = new PageClass($total,$curPage,$showNum,$pageLink);
		$limitStart = $page->limitStart;
		$this->smarty->assign("pageList",$page->pageList);
		$this->smarty->assign("cid",'2');/////////
		$sql = "select tech.*,tc.name as cateName from tbl_tech tech left join tbl_tech_cate tc on tech.cid=tc.id where tech.del_flg='0' $searchWhere limit $limitStart,$showNum";
		//echo $sql;
		$this->db->setQuery($sql);
		$rows = $this->db->loadList();
		foreach($rows as $key=>$value)
		{
			$rows[$key]['published'] = $value['published']=='1'?'已发布':'未发布';
		}
		$this->smarty->assign("rows",$rows);
		$this->smarty->assign("cateArr",$this->getTechCate());
		adminDisplay("admin_tech_list.tpl");
	}
	
	function addtech(){
		$sql = "select * from tbl_tech_cate where del_flg='0'";
		$this->db->setQuery($sql);
		$cateArr = $this->db->loadResultList('id','name');
		$this->smarty->assign("cateArr",$cateArr);
		if(isset($_REQUEST['id']) && intval($_REQUEST['id']) > 0){
			$id = intval($_REQUEST['id']);
			$sql = "select tech.*,con.content from tbl_tech tech left join tbl_tech_content con on con.techId=tech.id where tech.id='$id'";
			$row = $this->db->loadRow($sql);
			if(!empty($row)){
				$row['cateName'] = $cateArr[$row['cid']];
				$row['content'] = htmlspecialchars($row['content']);
				$this->smarty->assign("row",$row);
			}
		}
		adminDisplay("admin_tech_add.tpl");
	}
	
	function savetech(){
		$temprow = array();
		if (!get_magic_quotes_gpc()) {
			foreach($_POST as $key=>$value){
				$_POST[$key] = addslashes($value);
			}
		} 
		$temprow['title'] = htmlspecialchars(trim($_POST['title']));
		$temprow['cid'] = intval($_POST['cid']);
		$temprow['keywords'] = trim($_POST['keywords']);
		$temprow['published'] = $_POST['published'];
		$temprow['authorId'] = "admin";
		$temprow['authorName'] = "百企编辑部";
		$content = $_POST['content'];
		if(isset($_POST['published']) && $_POST['published'] == '1'){
			$temprow['published'] = "1";
		}
		if($_POST['id'] != "" && intval($_POST['id']) > 0){
			$temprow['mTime'] = time();
			$id = intval($_POST['id']);
			$where = " where id='$id'";
			$this->db->update($temprow,'tbl_tech', $where);
			$where2 = " where techId='$id'";
			$temprow2['content'] = $content;
			$this->db->update($temprow2,'tbl_tech_content', $where2);
		}else{
			$temprow['addTime'] = time();
			$temprow['mTime'] = time();
			$techId = $this->db->insert($temprow,'tbl_tech');
			if($techId > 0){
				$temprow2['techId'] = $techId;
				$temprow2['content'] = $content;
				$this->db->insert($temprow2,'tbl_tech_content');
			}
		}
		$_SESSION['sysMsg'] = '编辑成功';
		$this->index();
	}
	
	function deltech(){
		$id = intval($_REQUEST['id']);
		if($id > 0){
			$sql = "update tbl_tech set del_flg='1' where id='$id'";
			$this->db->query($sql);
			echo $id;
		}
		die;
	}

	function getTechCate(){
		$sql = "select * from tbl_tech_cate where del_flg='0'";
		$this->db->setQuery($sql);
		$rows = $this->db->loadResultList('id','name');
		return $rows;
	}

	function searchWhere(){
		$searchWhere = "";
		if(isset($_SESSION['search']['s_id']) && !isset($_POST['s_id'])){
			$_POST['s_id'] = $_SESSION['search']['s_id'];
		}
		if(isset($_SESSION['search']['s_title']) && !isset($_POST['s_title'])){
			$_POST['s_title'] = $_SESSION['search']['s_title'];
		}
		if(isset($_SESSION['search']['s_cid']) && !isset($_POST['s_cid'])){
			$_POST['s_cid'] = $_SESSION['search']['s_cid'];
		}
		if(isset($_SESSION['search']['s_published']) && !isset($_POST['s_published'])){
			$_POST['s_published'] = $_SESSION['search']['s_published'];
		}
		if(isset($_SESSION['search']['s_time_1']) && !isset($_POST['s_time_1'])){
			$_POST['s_time_1'] = $_SESSION['search']['s_time_1'];
		}
		if(isset($_SESSION['search']['s_time_2']) && !isset($_POST['s_time_2'])){
			$_POST['s_time_2'] = $_SESSION['search']['s_time_2'];
		}

		if(isset($_POST['s_id']) && intval($_POST['s_id'])>0){
			$searchWhere .= " AND tech.id='".intval($_POST['s_id'])."'";
			$_SESSION['search']['s_id'] = intval($_POST['s_id']);
		}
		if(isset($_POST['s_title']) && trim($_POST['s_title'])!=""){
			$searchWhere .= " AND tech.title LIKE '%".$_POST['s_title']."%'";
			$_SESSION['search']['s_title'] = trim($_POST['s_title']);
		}
		if(isset($_POST['s_cid']) && intval($_POST['s_cid'])>0){
			$searchWhere .= " AND tech.cid='".intval($_POST['s_cid'])."'";
			$_SESSION['search']['s_cid'] = intval($_POST['s_cid']);
		}
		if(isset($_POST['s_published']) && ($_POST['s_published']=='0' || $_REQUEST['s_published']=='1')){
			$searchWhere .= " AND tech.published='".intval($_POST['s_published'])."'";
			$_SESSION['search']['s_published'] = intval($_POST['s_published']);
		}
		if(isset($_POST['s_time_1']) && $_POST['s_time_1']!=""){
			$searchWhere .= " AND tech.addTime>='".strtotime($_POST['s_time_1'])."'";
			$_SESSION['search']['s_time_1'] = $_POST['s_time_1'];
		}
		if(isset($_POST['s_time_2']) && $_POST['s_time_2']!=""){
			$searchWhere .= " AND tech.addTime<='".intval(strtotime($_POST['s_time_2'])+86400)."'";
			$_SESSION['search']['s_time_2'] = $_POST['s_time_2'];
		}

		return $searchWhere;
	}

	function __destruct()
	{
		//unset($_SESSION['search']);
	}
}
$tech = new Tech();

?>